﻿using System.Linq;
using System.Web;
using System.Web.Mvc;
using JSC.Core;
using JSC.Portal.Entities;
using JSC.Portal.Module;
using JSC.Portal.Services;
using JSC.Portal.Web.Filters;
using JSC.Portal.Web.Helper;
using JSC.Portal.Web.Models;

namespace JSC.Portal.Web.Controllers
{
    public class PortalEngineController : Controller
    {
        [Compress]
        //[OutputCache(Duration = int.MaxValue, VaryByParam = "language;friendlyurl")]
        public ActionResult ShowFriendlyUrl(string language, string friendlyurl)
        {
            IPortalPageService svc = FacadeFactory.CreateFacade<IPortalPageService>();
            string siteAddress = Url.GetHostUrl();

            PortalPage page = null;
            if (!string.IsNullOrEmpty(language))
            {
                page = (friendlyurl.Equals("/"))
                    ? svc.GetRootPortalPage(siteAddress, language) : svc.GetPortalPageByFriendlyURL(siteAddress, language, friendlyurl);
            }
            if (page == null)
            {
                IPortalSiteService ssvc = FacadeFactory.CreateFacade<IPortalSiteService>();
                PortalSite site = ssvc.GetPortalSiteByUrl(siteAddress);
                page = site.PortalPages.FirstOrDefault(x => x.Language == site.DefaultLanguage);
                if (page == null)
                {
                    page = site.PortalPages.FirstOrDefault();
                }
            }
            
            return Show(page);
        }

        private ActionResult Show(PortalPage page)
        {
            TemplatedViewData data = new TemplatedViewData();

            HttpCookie c = new HttpCookie("JSC.Culture", page.Language);
            Response.Cookies.Add(c);

            data.Language = page.Language;
            data.Template = page.Template;
            data.Title = page.Name;
            data.CurrentPage = page;
            data.CSS = Url.EnsureTilde(Url.Combine(page.Template.Path, "CSS", page.Template.CSS));

            foreach (Section s in page.Sections)
            {
                ModuleBase module = ModuleBase.GetModuleFromSection(s);
                module.Load();
                data.Modules.Add(module);
            }
            return View("PortalView", data);
        }

        [Compress]
        //[OutputCache(Duration = int.MaxValue, VaryByParam = "sectionid")]
        public ActionResult ShowSection(int sectionid)
        {
            ISectionService svc = FacadeFactory.CreateFacade<ISectionService>();
            Section s = svc.GetById(sectionid, false);
            TemplatedViewData data = new TemplatedViewData();
            data.Language = s.Page.Language;
            data.Template = s.Page.Template;
            data.Title = s.Name;
            data.CurrentPage = s.Page;
            data.CSS = Url.EnsureTilde(Url.Combine(s.Page.Template.Path, "CSS", s.Page.Template.CSS));

            HttpCookie c = new HttpCookie("JSC.Culture", data.Language);
            Response.Cookies.Add(c);

            ModuleBase module = ModuleBase.GetModuleFromSection(s);
            module.Load();
            data.Modules.Add(module);
            return View("PortalView", data);
        }
    }
}
